using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace My.IdlConverter.Core
{
    public class MethodCollisionManager
    {
        private static MethodCollisionManager _manager;
        private IList<Type> _collisionTypes = new List<Type>();

        static MethodCollisionManager()
        {
            _manager = new MethodCollisionManager();
        }

        public static MethodCollisionManager Instance
        {
            get
            {
                return _manager;
            }
        }

        public void RegisterCollisition(Type typeCollision)
        {
            if (!_collisionTypes.Contains(typeCollision))
                _collisionTypes.Add(typeCollision); 
        }

        public void RemoveCollision(Type typeCollision)
        {
            if (_collisionTypes.Contains(typeCollision))
            {
                _collisionTypes.Remove(typeCollision);
            }
        }

        public T GetCollision<T>() where T : class
        {
            foreach (Type typeCollision in _collisionTypes)
            {
                if (typeCollision == typeof(T))
                {
                    T collision = Activator.CreateInstance<T>();
                    return collision;
                }
            }
            Debug.Assert(false);
            return null;
        }
    }
}
